from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime

class WebAuthnRegisterBeginRequest(BaseModel):
    """开始注册WebAuthn凭证的请求"""
    name: Optional[str] = Field(None, max_length=100, description="凭证名称，如'YubiKey'")

class WebAuthnRegisterBeginResponse(BaseModel):
    """开始注册的响应"""
    options: dict  # WebAuthn注册选项

class WebAuthnRegisterCompleteRequest(BaseModel):
    """完成注册WebAuthn凭证的请求"""
    name: Optional[str] = Field(None, max_length=100)
    credential: dict  # 浏览器返回的凭证数据

class WebAuthnLoginBeginRequest(BaseModel):
    """开始WebAuthn登录的请求（支持无用户名的驻留凭证登录）"""
    username: Optional[str] = Field(None, min_length=1)

class WebAuthnLoginBeginResponse(BaseModel):
    """开始登录的响应"""
    options: dict  # WebAuthn登录选项

class WebAuthnLoginCompleteRequest(BaseModel):
    """完成WebAuthn登录的请求（支持通过凭证ID识别用户）"""
    username: Optional[str] = Field(None, min_length=1)
    credential: dict  # 浏览器返回的凭证数据

class CredentialResponse(BaseModel):
    """凭证信息响应"""
    id: int
    name: Optional[str]
    created_at: datetime
    last_used_at: Optional[datetime]
    is_active: bool
    
    class Config:
        from_attributes = True

class WebAuthnUnbindRequest(BaseModel):
    """解绑硬件密钥的请求"""
    verification_token: str = Field(..., description="验证令牌")

class CredentialListResponse(BaseModel):
    """凭证列表响应"""
    credentials: List[CredentialResponse]
    count: int